<!--历史数据-->
<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>

            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
                <a-button @click="downloadFile">下载</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="alertLogId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>

          <template #expandedRowRender="{ column, record }">
            <a-table :columns="innerColumns" :data-source="formatExceptionDataTab(record)" :pagination="false" />
          </template>

        </ele-pro-table>
      </a-card>
    </div>
  </div>
</template>

<script>
import { message } from 'ant-design-vue';
import {MonitorAlertsLogApi} from "@/api/monitors/MonitorAlertsLogApi";
import { SysDatadownLogApi } from '@/api/system/operation/SysDatadownLogApi';

export default {
  name: 'CattleManagerCattleDiseasesData',
  components: {},
  props: {
    data: Object
  },
  async mounted() {

  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '告警详情',
          dataIndex: 'alertsText'
        },
        {
          title: '最近异常值',
          dataIndex: 'alertsValue'
        },
        {
          title: '持续几次',
          dataIndex: 'alertsTime'
        },
        {
          title: '触发开始时间',
          dataIndex: 'alertStartTime'
        },
        {
          title: '触发结束时间',
          dataIndex: 'alertEndTime'
        },
        {
          title: '标签',
          dataIndex: 'tags'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      innerColumns : [
        {
          title: '触发时间',
          dataIndex: 'time'
        },
        {
          title: '呼吸速率(次/min)',
          dataIndex: 'respirationRate'
        },
        {
          title: '心跳速率(次/min)',
          dataIndex: 'heartRate'
        },
      ],
      // 表格搜索条件
      where: {
        thirdId : this.data.cattleId
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
    };
  },
  methods: {
    formatExceptionDataTab(record){
      let alertsData = record?.alertsData;
      let formatDatas = [];
      if (alertsData != undefined && alertsData != null) {
        let alertDataItems = JSON.parse((alertsData || "[]"));
        if (alertDataItems.length > 0) {
          for (let i = 0; i < alertDataItems.length; i++) {
            formatDatas.push({
              "time" : alertDataItems[i]?.time,
              "respirationRate" : alertDataItems[i]?.respiration_rate,
              "heartRate" : alertDataItems[i]?.heart_rate,
            });
          }
        }
      }
      return formatDatas;
    },
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({page: 1});
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    reset() {
      this.where.thirdId = this.data.cattleId;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    downloadFile () {
      if (this.where.searchBeginTime && this.where.searchEndTime) {
        let fromParam = {
          "searchBeginTime" : this.where.searchBeginTime,
          "searchEndTime" : this.where.searchEndTime,
          "thirdId" : this.data.cattleId,
        };
        let from = {
          "downName" : "奶牛id为"+this.data.cattleId+"的疾病数据下载",
          "downScene" : 1,
          "downCondition" : JSON.stringify(fromParam)
        };

        let result = SysDatadownLogApi.add(from);
        result
          .then(result1 => {
            message.success("添加下载任务成功，请移步到【数据下载】->【导出记录】查看")
          })
          .catch(() => {
          });
      } else {
        message.warn("请选择时间条件");
      }
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/03/18 20:10
     */
    async remove(row) {
      const result = await MonitorAlertsLogApi.delete({alertLogId: row.alertLogId});
      message.success(result.message);
      this.reload();
    },

    datasource({ page, limit, where, orders }) {
      return MonitorAlertsLogApi.findCowAlertLogPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
